﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Volo.Abp.Application.Services;
using Volo.Abp.Application.Dtos;
using Microsoft.AspNetCore.Mvc;
using Volo.Abp.Domain.Repositories;
using System.Linq.Dynamic.Core;
using Wz.Interface;
using Wz.Entities;
using Wz.Dto;

namespace Wz.Services
{
    public class WarehousingService : ApplicationService, IWarehousingService
    {
        //依赖注入
        private readonly IRepository<Warehousing, Guid> _warehousings;
        public WarehousingService(IRepository<Warehousing, Guid> _warehousingss)
        {
            _warehousings = _warehousingss;
        }


        /// <summary>
        /// 入库显示
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// 
        [HttpGet]
        public async Task<PagedResultDto<WarehousingDto>> WarehousingShow(PagedAndSortedResultRequestDto input)
        {
            //创建查询
            var query = _warehousings.OrderBy(input.Sorting);
            //获取总条数
            var count = await query.CountAsync();
            //获取实体
            List<Warehousing> warehousings = await _warehousings.ToListAsync();
            //实体映射
            List<WarehousingDto> warehousingDtos = ObjectMapper.Map<List<Warehousing>, List<WarehousingDto>>(warehousings);
            //返回结果
            return new PagedResultDto<WarehousingDto>(count, warehousingDtos);
        }

        /// <summary>
        /// 入库添加
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        /// 
        [HttpPost]
        public async Task<WarehousingDto> WarehousingAdd([FromForm] AddWarehousingDto obj)
        {
            Warehousing warehousing = new Warehousing(
                    GuidGenerator.Create(),  //Guid
                    DateTime.Now,    //当前时间
                    obj.CreateTime = DateTime.Now,
                    obj.Registrant,
                    obj.Deleted,
                    obj.ProName,
                    obj.PayType,
                    obj.ProType,
                    obj.Material,
                    obj.Specs,
                    obj.Productions1,
                    obj.Productions2,
                    obj.Price,
                    obj.Num,
                    obj.Remarks
                );
            var result = await _warehousings.InsertAsync(warehousing);
            return ObjectMapper.Map<Warehousing, WarehousingDto>(result);
        }

        /// <summary>
        /// 入库删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// 
        [HttpDelete]
        public async Task WarehousingDel(Guid id)
        {
            await _warehousings.DeleteAsync(id);
        }

        /// <summary>
        /// 入库回显
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// 
        [HttpGet]
        public async Task<WarehousingDto> WarehousingFind(Guid id)
        {
            var result = await _warehousings.FindAsync(id);
            return ObjectMapper.Map<Warehousing, WarehousingDto>(result);
        }

        /// <summary>
        /// 入库修改
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        /// 
        [HttpPut]
        public async Task<WarehousingDto> WarehousingUpt([FromForm] UpdateWarehousingDto obj)
        {
            Warehousing warehousing = new Warehousing(
                    obj.ID,  //Guid
                    DateTime.Now,    //当前时间
                    obj.CreateTime,
                    obj.Registrant,
                    obj.Deleted,
                    obj.ProName,
                    obj.PayType,
                    obj.ProType,
                    obj.Material,
                    obj.Specs,
                    obj.Productions1,
                    obj.Productions2,
                    obj.Price,
                    obj.Num,
                    obj.Remarks
                );
            var result = await _warehousings.UpdateAsync(warehousing);
            return ObjectMapper.Map<Warehousing, WarehousingDto>(result);
        }
    }
}
